package com.apple1.blogservices.impl;

import com.apple1.blogdao.CommentMapper;
import com.apple1.blogentity.Comment;
import com.apple1.blogentity.CommentExample;
import com.apple1.blogentity.vo.CommentVo;
import com.apple1.blogservices.CommentService;
import com.apple1.commonutil.BeanMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author chencheng
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    CommentMapper commentMapper;

    /**
     * 方法名: 添加评论
     * 处理逻辑:
     * note(注意):
     * @param commentVo
     * @return int 0-失败 非0-成功
     */
    @Override
    public int addComment(CommentVo commentVo) {
        if (commentVo == null){
            return 0;
        }
        Comment comment = BeanMapUtil.getMapBean(commentVo,Comment.class);
        int i = commentMapper.insertSelective(comment);

        return i;
    }
    /**
     * 方法名: 通过评论id删除评论
     * 处理逻辑: 1.通过评论id查到评论的信息 2.更新评论的删除标志位
     * note(注意):
     * @param comment_id
     * @return int
     */
    @Override
    public int delComment(Integer comment_id) {
        if (comment_id == null){
            return 0;
        }

        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andTableIdEqualTo(comment_id);

        List<Comment> comments = commentMapper.selectByExample(commentExample);
        if (comments == null || comments.size() == 0){
           return 0;
        }
        Comment comment = comments.get(0);
        comment.setStatus(1);
        comment.setTableId(null);
        return commentMapper.updateByExampleSelective(comment,commentExample);
    }
    /**
     * 方法名: 通过评论id，更新评论
     * 处理逻辑:
     * note(注意): 这个是为管理员准备的接口
     * @param comment_id
     * @param commentVo
     * @return int
     */
    @Override
    public int updateCommentById(Integer comment_id, CommentVo commentVo) {
        if (comment_id == null || commentVo == null){
            return 0;
        }

        Comment comment = BeanMapUtil.getMapBean(commentVo,Comment.class);

        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andTableIdEqualTo(comment_id);

        comment.setTableId(null);

        return commentMapper.updateByExampleSelective(comment,commentExample);


    }
    /**
     * 方法名: 通过文章id查询所有与文章相关的评论
     * 处理逻辑:
     * note(注意):
     * @param article_id
     * @return java.util.List<com.apple1.blogentity.vo.CommentVo>
     */
    @Override
    public List<CommentVo> selectCommentsByArticleId(Integer article_id) {

        if (article_id == null){
            return null;
        }
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andArticleIdEqualTo(article_id);

        List<Comment> comments = commentMapper.selectByExample(commentExample);

        List<CommentVo> comments1 = null;
        if (comments != null || comments.size() !=0){
            comments1 =  new ArrayList<>();

            for (Comment comment : comments){
                comments1.add(BeanMapUtil.getMapBean(comment,CommentVo.class));
            }

        }
        return comments1;
    }

    /**
     * 方法名: 通过评论id查询评论信息
     * 处理逻辑:
     * note(注意):
     * @param comment_id
     * @return com.apple1.blogentity.vo.CommentVo
     */
    @Override
    public CommentVo selectCommentByTableId(Integer comment_id) {
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andTableIdEqualTo(comment_id);

        List<Comment> comments = commentMapper.selectByExample(commentExample);
        if (comments == null || comments.size() == 0){
            return null;
        }
        Comment comment = comments.get(0);

        return BeanMapUtil.getMapBean(comment,CommentVo.class);
    }
    /**
     * 方法名: 根据评论id添加评论喜欢数
     * 处理逻辑:
     * note(注意): 没有限制，每个人都可以不停地增加评论的喜欢数目
     * @param comment_id
     * @return int
     */
    @Override
    public int addCommentLikeById(Integer comment_id) {
        CommentVo commentVo = selectCommentByTableId(comment_id);
        if (commentVo == null){
            return 0;
        }
        commentVo.setLike(commentVo.getLike()+1);

        int i = updateCommentById(comment_id, commentVo);

        return i;
    }

    /**
     * 方法名: 减少评论的喜欢数
     * 处理逻辑:
     * note(注意):
     * @param comment_id
     * @return int
     */
    @Override
    public int reduceCommentLikeById(Integer comment_id) {

        CommentVo commentVo = selectCommentByTableId(comment_id);
        if (commentVo == null){
            return 0;
        }
        commentVo.setLike(commentVo.getLike()-1);

        int i = updateCommentById(comment_id, commentVo);

        return i;

    }
    /**
     * 方法名: 增加评论的不喜欢数
     * 处理逻辑:
     * note(注意):
     * @param comment_id
     * @return int
     */
    @Override
    public int addCommentDislikeById(Integer comment_id) {
        CommentVo commentVo = selectCommentByTableId(comment_id);
        if (commentVo == null){
            return 0;
        }
        commentVo.setDislike(commentVo.getDislike()+1);

        int i = updateCommentById(comment_id, commentVo);
        return i;
    }
    /**
     * 方法名: 减少评论的不喜欢数
     * 处理逻辑:
     * note(注意):
     * @param comment_id
     * @return int
     */
    @Override
    public int reduceCommentDislikeById(Integer comment_id) {
        CommentVo commentVo = selectCommentByTableId(comment_id);
        if (commentVo == null){
            return 0;
        }
        commentVo.setDislike(commentVo.getDislike()-1);

        int i = updateCommentById(comment_id, commentVo);
        return i;
    }
}
